package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.member.dao.MemberUserGroupDao;
import com.bestcem.xm.member.dao.mapper.MemberUserGroupMapper;
import com.bestcem.xm.member.entity.mysql.MemberUserGroupEntity;
import com.bestcem.xm.member.entity.pojo.MemberUserGroupDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dc.wang <dc.wang@idiaoyan.com>
 * @version V1.0
 * @date 2022/2/11 16:38
 */
@Slf4j
@Repository
public class MemberUserGroupDaoImpl implements MemberUserGroupDao {
    @Autowired
    private MemberUserGroupMapper memberUserGroupMapper;

    @Override
    public List<MemberUserGroupDO> findByIds(Collection<String> ids) {
        List<MemberUserGroupEntity> entities = memberUserGroupMapper.findByIds(ids);
        return entities.stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public Integer countSubAndInactiveGroup(String orgId, Collection<String> storeIds) {
        String groupIds = JSONObject.toJSONString(storeIds);
        return memberUserGroupMapper.countSubAndInactiveGroup(orgId, groupIds);
    }

    @Override
    public List<MemberUserGroupDO> allSubAndInactiveGroup(String orgId, Collection<String> storeIds) {
        String groupIds = JSONObject.toJSONString(storeIds);
        List<MemberUserGroupEntity> entities = memberUserGroupMapper.allSubAndInactiveGroup(orgId, groupIds);
        return entities.stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public void batchInsertReplace(Collection<MemberUserGroupDO> memberUserGroups) {
        List<MemberUserGroupEntity> entities = memberUserGroups.stream().map(this::convert).collect(Collectors.toList());
        memberUserGroupMapper.batchInsertReplace(entities);
    }

    @Override
    public List<String> listByParentIn(String store) {
        List<MemberUserGroupEntity> memberUserGroupEntities = memberUserGroupMapper.listByParentIn(store);
        if (CollUtil.isNotEmpty(memberUserGroupEntities)) {
            return memberUserGroupEntities.stream().map(MemberUserGroupEntity::getGroupId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    private MemberUserGroupDO convert(MemberUserGroupEntity entity) {
        MemberUserGroupDO dto = new MemberUserGroupDO();
        dto.setGroupId(entity.getGroupId());
        dto.setOrgId(entity.getOrgId());
        dto.setStatus(entity.getStatus());
        dto.setParentIds(JSONObject.parseArray(entity.getParentIds(), String.class));
        dto.setUpdateTime(entity.getCreateTime());
        dto.setUpdateTime(entity.getUpdateTime());
        return dto;
    }

    private MemberUserGroupEntity convert(MemberUserGroupDO dto) {
        MemberUserGroupEntity entity = new MemberUserGroupEntity();
        entity.setGroupId(dto.getGroupId());
        entity.setOrgId(dto.getOrgId());
        entity.setStatus(dto.getStatus());
        entity.setParentIds(JSONObject.toJSONString(dto.getParentIds()));
        entity.setCreateTime(dto.getCreateTime());
        entity.setUpdateTime(dto.getUpdateTime());
        return entity;
    }
}
